const http = require("http");
const fs = require("fs");
const url = require("url");

// 创建服务器
// 获取服务器实例
const server = http.createServer();
server.listen(9999, function () {
  console.log("http://127.0.0.1:9999");
});

server.on("request", function (req, res) {
  // res.setHeader("Content-type", "text/plain;charset=utf-8");
  // res.setHeader("Content-type", "text/html;charset=utf-8");
  // res.write("你好");
  // res.write("<h1>你好</h1>");
  // res.end();
  console.log(req.method, "method");
  if (req.method === "GET") {
    // if () { }
    console.log(url.parse(req.url, true).query.id, "fffffffffffff");
    if (req.url == "/") {
      fs.readFile("./index.html", "utf-8", function (err, data) {
        res.write(data);
        res.end();
      });
    } else {
      fs.readFile("./1.jpg", function (err, data) {
        res.end(data);
      });
    }
  } else if (req.method === "POST") {
    req.on("data", function (d) {
      console.log(d, "gg");
    });
    res.end();
  }
});
